//
//  PropertyListUtil.m
//  PigMarket
//
//  Created by Felix on 2019/4/11.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PropertyListUtil.h"

#import "NSData+XOR.h"

@implementation PropertyListUtil

+ (NSDictionary *)getRootWithName:(NSString *)name {
    if (![name containsString:@".plist"]) {
        name = [NSString stringWithFormat:@"%@.plist", name];
    }
    NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];
    NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:filePath];
    
    return root;
}


+ (NSNumber *)getDataWithRoot:(NSDictionary *)root
                          key:(NSString *)key {
    
    NSArray<NSString *> *keyArray = [key componentsSeparatedByString:@"."];
    
    NSDictionary *targetDic = root;
    
    NSInteger index = 0;
    NSInteger count = [keyArray count];
    while (index < (count - 1) ) {
        targetDic = targetDic[keyArray[index]];
        index++;
    }
    
    return targetDic[keyArray[count - 1]];
}

+ (NSDictionary *)decodePlistWithName:(NSString *)name {
    NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];
    NSString *resultStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSData *data = [[NSData alloc] initWithBase64EncodedString:resultStr options:0];
    NSData *decodeData = [data xorWithStringKey:@"naturalgrowthpigmarket666"];
    NSPropertyListFormat format;
    NSDictionary *dicData = [NSPropertyListSerialization propertyListWithData:decodeData options:NSPropertyListImmutable format:&format error:nil];
    return dicData;
}

@end
